﻿using System.Configuration;
using System.Web.Configuration;

namespace UFIDA.U9.WSS.ProxyService.Utils
{
    public static class ConfigurationHelper
    {
        private static Configuration _wsConfig;

        /// <summary>
        ///     获取配置
        /// </summary>
        /// <returns></returns>
        public static Configuration GetConfiguration()
        {
            return _wsConfig ?? (_wsConfig = WebConfigurationManager.OpenWebConfiguration(Constant.VirtualPath));
        }

        /// <summary>
        ///     获取参数配置值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetAppSettingValue(string key)
        {
            Configuration config = GetConfiguration();
            if (config.AppSettings.Settings[key] == null) return string.Empty;
            string strValue = config.AppSettings.Settings[key].Value;
            return string.IsNullOrWhiteSpace(strValue)
                ? string.Empty
                : strValue.Trim();
        }

        /// <summary>
        ///     Token有效时长(分钟)
        /// </summary>
        /// <returns></returns>
        public static int GetTokenEffectiveMinutes()
        {
            int tokenEffectiveMinutes = -1;
            string strTokenEffectiveMinutes =
                GetAppSettingValue(Constant.TokenEffectiveMinutesName);
            if (!string.IsNullOrEmpty(strTokenEffectiveMinutes))
            {
                int.TryParse(strTokenEffectiveMinutes, out tokenEffectiveMinutes);
            }

            return tokenEffectiveMinutes;
        }

        /// <summary>
        ///     获取Token是否需要用户认证
        /// </summary>
        /// <returns></returns>
        public static bool GetGetTokenNeedUserPassword()
        {
            string strGetTokenNeedUserPassword =
                GetAppSettingValue(Constant.GetTokenNeedUserPasswordName);
            return !string.IsNullOrEmpty(strGetTokenNeedUserPassword) &&
                   strGetTokenNeedUserPassword.ToLower() == "true";
        }
    }
}